using System.ComponentModel.Design;
using PrismFactory.SoftwareFactory.Views.Properties;
using PrismFactory.SoftwareFactory.Views.Validators;

namespace PrismFactory.SoftwareFactory.Views.GenerateViewClasses
{
    public class GenerateViewClassesPageViewModel : ViewModelBase
    {
        public const string ViewNameKey = "ViewName";
        public const string ViewModelNameKey = "ViewModelName";
        public const string ViewModelInterfaceNameKey = "ViewModelInterfaceName";
        public const string ViewModelTestClassNameKey = "ViewModelTestClassName";
        public const string GenerateCodeTemplatesKey = "GenerateCodeTemplates";

        private readonly IDictionaryService dictionaryService;

        public GenerateViewClassesPageViewModel(IDictionaryService dictionaryService)
        {
            this.dictionaryService = dictionaryService;
            ValidatedProperties = new[]
                                      {
                                          ViewNameKey,
                                      };
            BuildNames();
        }

        public string ViewName
        {
            get { return (string) dictionaryService.GetValue(ViewNameKey); }
            set
            {
                dictionaryService.SetValue(ViewNameKey, value);
                BuildNames();
                OnPropertyChanged(ViewNameKey);
            }
        }

        public string ViewModelName
        {
            get { return (string) dictionaryService.GetValue(ViewModelNameKey); }
            set
            {
                dictionaryService.SetValue(ViewModelNameKey, value);
                OnPropertyChanged(ViewModelNameKey);
            }
        }

        public string ViewModelInterfaceName
        {
            get { return (string) dictionaryService.GetValue(ViewModelInterfaceNameKey); }
            set
            {
                dictionaryService.SetValue(ViewModelInterfaceNameKey, value);
                OnPropertyChanged(ViewModelInterfaceNameKey);
            }
        }

        public string ViewModelTestClassName
        {
            get { return (string) dictionaryService.GetValue(ViewModelTestClassNameKey); }
            set
            {
                dictionaryService.SetValue(ViewModelTestClassNameKey, value);
                OnPropertyChanged(ViewModelTestClassNameKey);
            }
        }

        public bool GenerateCodeTemplates
        {
            get { return (bool) dictionaryService.GetValue(GenerateCodeTemplatesKey); }
            set
            {
                dictionaryService.SetValue(GenerateCodeTemplatesKey, value);
                OnPropertyChanged(GenerateCodeTemplatesKey);
            }
        }

        protected override void DoPropertyValidation(string propertyName, ref string error)
        {
            switch (propertyName)
            {
                case ViewNameKey:
                    var validator = new IdentifierValidator();
                    if (!validator.Validate(ViewName).IsValid)
                        error = Resources.InvalidIdentifier;
                    break;
            }
        }

        private void BuildNames()
        {
            ViewModelName = ViewName + "Model";
            ViewModelInterfaceName = "I" + ViewModelName;
            ViewModelTestClassName = ViewModelName + "Fixture";
        }
    }
}